package com.molyfun.parents.activity.store.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.report.iepPlan.IProductDetailTabCell;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 */
public class ProductDetailTabAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<T> mList;

    private int mCurrentTab = 0;

    private ProductDetailTabCallback mProductDetailTabCallback;

    public ProductDetailTabAdapter(Context context, ProductDetailTabCallback productDetailTabCallback) {
        this.mContext = context;
        this.mProductDetailTabCallback = productDetailTabCallback;
    }

    public void refreshData(List<T> list) {
        this.mList = list;
        notifyDataSetChanged();
    }


    public void clearAll(){
        if(mList != null){
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<T> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ProductBriefViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_product_detail_tab, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final IProductDetailTabCell productBean = (IProductDetailTabCell)mList.get(position);
        ProductBriefViewHolder productBriefViewHolder = (ProductBriefViewHolder)holder;
        productBriefViewHolder.mProductDetailTabName.setText(productBean.getName());
        if(mCurrentTab == position){
            productBriefViewHolder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_bg));
        }else{
            productBriefViewHolder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_transparent));
        }
        productBriefViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mProductDetailTabCallback != null){
                    mProductDetailTabCallback.onProductDetailTabClick(position);
                    mCurrentTab = position;
                    notifyDataSetChanged();
                }
            }
        });
    }

    public interface ProductDetailTabCallback{
        void onProductDetailTabClick(int position);
    }

    public class ProductBriefViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.product_detail_tab_name)
        TextView mProductDetailTabName;

        public ProductBriefViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }

}
